using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using UrielGuy.SyntaxHighlightingTextBox;
using DM4;
using sscore;

namespace DM4Client
{
    public static class Common
    {
        //класс для заполнения комбобокса
        public class ItemInfo
        {
            private readonly string _id;
            private readonly string _name;

            public ItemInfo(string id, string name)
            {
                _id = id;
                _name = name;
            }

            public override string ToString()
            {
                return _name;
            }

            public string Id
            {
                get
                {
                    return _id;
                }
            }
        }

        public static void FillComboWithSetId(DevExpress.XtraEditors.ComboBoxEdit cb, Dictionary<string, string> data, string id = DM4Consts.SDefaultNULLId, bool showEmptyItem = true)
        {
            int itemIndex = -1;
            cb.Properties.Items.Clear();

            if (showEmptyItem)
            {
                cb.Properties.Items.Add("");
            }

            foreach (KeyValuePair<string, string> de in data)
            {
                cb.Properties.Items.Add(de.Value);
                if (de.Key == id)
                    itemIndex = cb.Properties.Items.Count - 1;
            }

            cb.SelectedIndex = itemIndex == -1 ? 0 : itemIndex;

        }

        public static void FillComboWithSetId(DevExpress.XtraBars.BarEditItem cb, DevExpress.XtraEditors.Controls.ComboBoxItemCollection items, Dictionary<string, string> data, string id = DM4Consts.SDefaultNULLId, bool showEmptyItem = true)
        {
            ItemInfo item = null;
            items.Clear();

            if (showEmptyItem)
            {
                ItemInfo ii = new ItemInfo(DM4Consts.SDefaultNULLId, "");
                items.Add(ii);
                if (DM4Consts.SDefaultNULLId == id) item = ii;
            }

            foreach (KeyValuePair<string, string> de in data)
            {
                ItemInfo ii = new ItemInfo(de.Key, de.Value);
                items.Add(new ItemInfo(de.Key, de.Value));
                if (de.Key == id) item = ii;
            }
            cb.EditValue = item ?? (items.Count > 0 ? items[0] : null);
        }

        public static void FillComboWithSetId(DevExpress.XtraBars.BarEditItem cb, DevExpress.XtraEditors.Controls.ComboBoxItemCollection items, Dictionary<string, ParamEntry> data, string id = DM4Consts.SDefaultNULLId, bool showEmptyItem = true)
        {
            ItemInfo item = null;
            items.Clear();

            if (showEmptyItem)
            {
                ItemInfo ii = new ItemInfo(DM4Consts.SDefaultNULLId, "");
                items.Add(ii);
                if (DM4Consts.SDefaultNULLId == id) item = ii;
            }

            foreach (KeyValuePair<string, ParamEntry> de in data)
            {
                ItemInfo ii = new ItemInfo(de.Key, de.Value);
                items.Add(new ItemInfo(de.Key, de.Value));
                if (de.Key == id) item = ii;
            }
            cb.EditValue = item ?? (items.Count > 0 ? items[0] : null);
        }

        public static string GetIdByComboValue(DevExpress.XtraEditors.ComboBoxEdit cb, Dictionary<string, string> data)
        {
            string id = "-1";

            string value = cb.Properties.Items[cb.SelectedIndex].ToString();

            if (data.ContainsValue(value))
                foreach (KeyValuePair<string, string> de in data)
                {
                    if (de.Value == value)
                    {
                        if (de.Value != "")
                            id = de.Key;
                        break;
                    }
                }

            return id;
        }

        public static string GetIdByComboValue(DevExpress.XtraBars.BarEditItem cb, Dictionary<string, string> data)
        {
            string id = "-1";

            string value = cb.EditValue.ToString();

            if (data.ContainsValue(value))
                foreach (KeyValuePair<string, string> de in data)
                {
                    if (de.Value == value)
                    {
                        if (de.Value != "")
                            id = de.Key;
                        break;
                    }
                }

            return id;
        }

        public static SyntaxHighlightingTextBox CreateScriptEditControl(Control.ControlCollection cc, Color backColor, bool readOnly = false)
        {
            SyntaxHighlightingTextBox shtb = new SyntaxHighlightingTextBox
            {
                Location = new Point(0, 0),
                Dock = DockStyle.Fill,
                BorderStyle = BorderStyle.None,
                ReadOnly = readOnly,
                BackColor = backColor
            };

            shtb.Seperators.Add(' ');
            shtb.Seperators.Add('\r');
            shtb.Seperators.Add('\n');
            shtb.Seperators.Add(',');
            shtb.Seperators.Add('.');
            shtb.Seperators.Add('+');
            shtb.Seperators.Add(';');
            shtb.Seperators.Add('(');
            shtb.Seperators.Add(')');
            shtb.Seperators.Add('[');
            shtb.Seperators.Add(']');
            shtb.Seperators.Add('=');

            Font fnt = new Font("Courier New", 10, new FontStyle(), GraphicsUnit.Pixel, 240);
            shtb.Font = fnt;

            cc.Add(shtb);

            shtb.WordWrap = false;
            shtb.ScrollBars = RichTextBoxScrollBars.Both;// & RichTextBoxScrollBars.ForcedVertical;

            shtb.FilterAutoComplete = false;

            shtb.HighlightDescriptors.Add(new HighlightDescriptor("TASK", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("SUBTASK", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("RUN", Color.DarkRed, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("DECLARE", Color.DarkRed, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("PRIORITY", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("SESSIONS", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("MODULE", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("IGNOREERRORS", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("SESSIONCOUNT", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("PRIORITY", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("IF", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("ELSEIF", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("ELSE", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("BREAK", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));

            shtb.HighlightDescriptors.Add(new HighlightDescriptor("--", "", Color.Magenta, fnt, DescriptorType.ToEOL, DescriptorRecognition.StartsWith, false));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("/*", "*/", Color.Magenta, fnt, DescriptorType.ToCloseToken, DescriptorRecognition.StartsWith, false));

            shtb.HighlightDescriptors.Add(new HighlightDescriptor("ON", ":", Color.DarkViolet, fnt, DescriptorType.ToCloseToken, DescriptorRecognition.StartsWith, false));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("[", "]", Color.DarkBlue, fnt, DescriptorType.ToCloseToken, DescriptorRecognition.StartsWith, false));

            return shtb;
        }

        public static SyntaxHighlightingTextBox CreateMailEditTemplateControl(Control.ControlCollection cc, Color backColor, bool readOnly = false)
        {
            SyntaxHighlightingTextBox shtb = new SyntaxHighlightingTextBox
            {
                Location = new Point(0, 0),
                Dock = DockStyle.Fill,
                BorderStyle = BorderStyle.None,
                ReadOnly = readOnly,
                BackColor = backColor

            };

            shtb.Seperators.Add(' ');
            shtb.Seperators.Add('\r');
            shtb.Seperators.Add('\n');
            shtb.Seperators.Add(',');
            shtb.Seperators.Add('.');
            shtb.Seperators.Add('+');
            shtb.Seperators.Add(';');

            Font fnt = new Font("Courier New", 10, new FontStyle(), GraphicsUnit.Pixel, 240);
            shtb.Font = fnt;

            cc.Add(shtb);

            shtb.WordWrap = false;
            shtb.ScrollBars = RichTextBoxScrollBars.Both;// & RichTextBoxScrollBars.ForcedVertical;

            shtb.FilterAutoComplete = false;

            //shtb.HighlightDescriptors.Add(new HighlightDescriptor("TASK", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            //shtb.HighlightDescriptors.Add(new HighlightDescriptor("SUBTASK", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            //shtb.HighlightDescriptors.Add(new HighlightDescriptor("RUN", Color.DarkRed, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            //shtb.HighlightDescriptors.Add(new HighlightDescriptor("DECLARE", Color.DarkRed, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            //shtb.HighlightDescriptors.Add(new HighlightDescriptor("PRIORITY", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            //shtb.HighlightDescriptors.Add(new HighlightDescriptor("SESSIONS", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            //shtb.HighlightDescriptors.Add(new HighlightDescriptor("MODULE", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            //shtb.HighlightDescriptors.Add(new HighlightDescriptor("IF", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            //shtb.HighlightDescriptors.Add(new HighlightDescriptor("ELSEIF", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            //shtb.HighlightDescriptors.Add(new HighlightDescriptor("ELSE", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));
            //shtb.HighlightDescriptors.Add(new HighlightDescriptor("BREAK", Color.Green, fnt, DescriptorType.Word, DescriptorRecognition.WholeWord, true));


            shtb.HighlightDescriptors.Add(new HighlightDescriptor("[", "]", Color.Magenta, fnt, DescriptorType.ToCloseToken, DescriptorRecognition.StartsWith, false));
            shtb.HighlightDescriptors.Add(new HighlightDescriptor("<", ">", Color.DarkBlue, fnt, DescriptorType.ToCloseToken, DescriptorRecognition.StartsWith, false));

            return shtb;
        }

        public static SyntaxHighlightingTextBox CreateScriptLogControl(Control.ControlCollection cc, Color backColor)
        {
            var shtb = new SyntaxHighlightingTextBox
            {
                Location = new Point(0, 0),
                Dock = DockStyle.Fill,
                BorderStyle = BorderStyle.None,
                BackColor = backColor,
                ReadOnly = true
            };

            shtb.Seperators.Add(' ');
            shtb.Seperators.Add('\r');
            shtb.Seperators.Add('\n');

            var fnt = new Font("Courier New", 10, new FontStyle(), GraphicsUnit.Pixel, 240);
            shtb.Font = fnt;

            cc.Add(shtb);

            shtb.WordWrap = false;
            shtb.ScrollBars = RichTextBoxScrollBars.Both;

            shtb.FilterAutoComplete = false;

            shtb.HighlightDescriptors.Add(new HighlightDescriptor("ERROR:", Color.Red, fnt, DescriptorType.ToEOL, DescriptorRecognition.WholeWord, true));
            return shtb;
        }

    }
}
